class Seiso::ImportMaster
  # Imports a node IP address into Seiso.
  #
  # Author:: Willie Wheeler (mailto:wwheeler@expedia.com)
  # Copyright:: Copyright (c) 2014-2016 Expedia, Inc.
  # License:: Apache 2.0
  class Importers::NodeIpAddressImporter < Importers::BaseImporter

    def initialize(api, rest_util)
      self.api = api
      self.rest_util = rest_util
      self.mapper = Mappers::NodeIpAddressMapper.new api
      self.repo_resource = api.nodeIpAddresses.get

      @search_resource = repo_resource.search.get
    end

    # Can't make these protected, because that prevents BaseImporter from seeing them.
#    protected

    # BaseImporter callback
    def to_search_params(doc_nip, context)

      # FIXME For new nodes, we shouldn't have to look these up at all: nodes own NIPs, so the NIPs would be new too.
      # So there ought to be a way to suppress NIP do-we-post-or-put lookups when we already know that we're creating a
      # new node (and hence NIP).
      #
      # There is a case where we do need to look up the NIP. It's where we have an existing NIP and we want to change
      # the IP address role. This feels a little weird to me since it seems like NIPs ought to be identified by
      # <node, ipAddressRole> rathen than by <node, ipAddress>, but that's not how we've been doing it. [WLW]

      {
        'node' => context['node'].name,
        'ipAddress' => doc_nip['ipAddress']
      }
    end

    # BaseImporter callback
    def find_resource(search_params)
      node = search_params['node']
      ip_addr = search_params['ipAddress']
      # The .get is necessary to replace the finder URI with the canonical URI.
      @search_resource.findByNodeNameAndIpAddress(node: node, ipAddress: ip_addr).get
    end
  end
end
